import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-forms.vue"; //弹框form
import CustomDialog from "@/components/HntManage/pouringOrder/WBSHuitangangbao/index.vue";
import CustomDialog1 from "@/components/rebarThicknessManage/rebarEntrustList/index.vue";//委托单列表页
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import actions from "@/actions";
import API from '@/utils/commonRequest.js'

import {
    getSteelProtectionTestTaskDetail, //详情
    saveSteelProtectionTestTask, //保存
    getCheckitemsList, //试验检测依据-获取试验依据，检测依据
    getMangEquipmentPageList, //主要仪器设备名称及编号
    // getCheckTypeByUserList
    getUserCompanyTreeList
} from "@/api/request.js";
export default {
    data() {
        return {
            visible: "",
            // WBS
            WBSShow: false,
            // WBS
            // 委托单列表
            isShow: false,
            // 委托单列表
            loading: false,
            isLock: false, //保存按钮是否可编辑
            formData: {
                fId: "", // 申请单fid //为空判断为新增
                fCompanyid: "",
                fCompanyname: "",
                fEngineeringname: "", // 工程名称
                fEngineeringid: "", // 工程id
                fEngineeringpart: "", // 工程部位
                fEngineeringpartid: "", // 工程部位id
                fPlantestdates: "", // 计划检测日期 年-月-日
                fTestcondition: "", // 试验条件
                fTestbasisid: "", // 检测依据id
                fTestbasisname: "", // 检测依据内容
                fTestbasisnameList: [], // 检测依据内容--页面展示
                fDecidebasisid: "", // 判定依据id
                fDecidebasisname: "", // 判定依据内容
                fDecidebasisnameList: [], // 判定依据内容--页面展示
                fEquipnameandnoid: "", // 检测仪器设备名称及编号id
                fEquipnameandno: "", // 检测仪器设备名称及编号
                fEquipnameandnoList: [], // 检测仪器设备名称及编号--页面展示
                fThicknessdesignvalue: "", // 保护层厚度设计值(mm)
                fAllowdeviation1: "", // 允许偏差(-mm)
                fAllowdeviation: "", // 允许偏差(+mm)
                fCorrectionfactor: "", // 修正因子(mm)
                fAuthorid: "", // 编制人ID
                fAuthorname: "", // 编制人名称
            },
            formObj: formJs.formObj,
            btnShow: true,
            currentDetail: null,
            timeYMD: null, //当前日期
            timeYmdHms: null,
            selected: [], //多选接值
            primaryInfo: null,
            userNature: null,
            getList: null,

        };
    },
    components: {
        publicForm,
        CustomDialog,
        CustomDialog1,
        FormLyout,
    },
    created() {
        this.getData();
    },

    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))
        this.statusNum = this.primaryInfo.status;
        this.getList = this.primaryInfo.getList;

        actions.onGlobalStateChange((state, prev) => {
            this.userInfo = prev.currentUser
        }, true);

        API.userNature().then((res) => {
            if (res.errcode == 0) {
                this.userNature = res.data.Nature;
                // console.log(78789,this.userNature );
                this.btnShow = true;
                this.formObj.map(item => {
                    item.disabled = false;
                });
                // this.formObj[0].disabled = true;
                if (this.statusNum == 1) {
                    this.getCheckitemsList("5"); //钢保试验依据
                    this.getCheckitemsList("6"); //钢保判定依据
                    this.getEquipment(); //主要仪器设备名称及编号

                    if (this.userNature == "施工单位") {
                        this.formObj[1].disabled = true;
                        this.getTree(0, this.primaryInfo.getList.fCompanyid);

                    } else if (this.userNature == "总监办") {
                        this.getTree(1, this.primaryInfo.getList.fprojectid);
                        this.formObj[1].disabled = false;
                    } else if (this.userNature == "中心试验室") {
                        this.getTender(this.primaryInfo.getList.fprojectid);
                        this.formObj[0].disabled = false;
                        this.formObj[1].disabled = false;
                    }
                    this.formObj[11].disabled = true;
                    this.formObj[2].inputButton = true;
                    this.formData.fCompanyid = this.primaryInfo.fCompanyid;
                    this.formData.fPlantestdates = this.timeYMD;

                    this.formData.fAuthorid = this.userInfo.id; // //编制人id
                    this.formData.fAuthorname = this.userInfo.userName; // //编制人
                } else if (this.statusNum == 3) {
                    if (this.userNature == "施工单位") {
                        this.getTree(0, this.primaryInfo.getList.fCompanyid);
                        this.formObj[1].disabled = true;
                    } else if (this.userNature == "总监办") {
                        this.getTree(1, this.primaryInfo.getList.fprojectid);
                        this.formObj[1].disabled = false;
                    } else if (this.userNature == "中心试验室") {
                        this.getTender(this.primaryInfo.getList.fprojectid);
                        this.formObj[0].disabled = false;
                        this.formObj[1].disabled = false;
                    }
                    this.getCheckitemsList("5"); //钢保试验依据
                    this.getCheckitemsList("6"); //钢保判定依据
                    this.getEquipment(); //主要仪器设备名称及编号
                    this.getDetail(this.primaryInfo.data);
                    this.formObj[11].disabled = true;
                    this.formObj[2].inputButton = true;
                } else if (this.statusNum == 7) {
                    this.getDetail(this.primaryInfo.data);

                    this.formObj[2].inputButton = false;
                    this.btnShow = false;
                    this.formObj.map(item => {
                        item.disabled = true;
                    });

                }
            }
        }).catch((err) => {
            console.log("API", err)
        })




    },
    destroyed() {
        window.sessionStorage.removeItem("GxEmbedded_primaryInfo");
    },
    methods: {
        getTree(num, id) {
            var data = {
                companyId: id,
                userId: this.userInfo.id
            }
            this.formObj[1].options = [];
            getUserCompanyTreeList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        if (num == 0) {//施工单位
                            this.formData.fCompanyname = companyTree[0].fShortname;
                            this.formData.fCompanyid = companyTree[0].fCompanyid;
                        } else if (num == 1) {//总监办
                            var info = this.commonJs.tranListToTreeData(companyTree)[0].children;
                            info.map(item => {
                                this.formObj[1].options.push({
                                    label: item.fShortname,
                                    value: item.fCompanyid
                                })
                            })
                            this.formData.fCompanyname = this.formObj[1].options[0].label;
                            this.formData.fCompanyid = this.formObj[1].options[0].value;
                        }

                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 中心实验室调标段
        async getTender(e) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                headers: {
                    "Content-Type": "application/json",
                    Authorization: localStorage.getItem("access_token"),
                },
            }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                    this.formObj[1].options = [];
                    data.map((item) => {
                        this.formObj[1].options.push({
                            label: item.sectionName,
                            value: item.sectionId,
                        });
                    });
                    this.formData.fCompanyname = this.formObj[1].options[0].label;
                    this.formData.fCompanyid = this.formObj[1].options[0].value;

                }
            });
        },
        refreshList() { },
        // 详情
        getDetail(row) {
            this.loading = true;
            getSteelProtectionTestTaskDetail({
                fId: row.fId
            }).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var that = this;
                    that.formData = res.data.testTask;
                    if (that.statusNum != 7) {
                        if (row.fStatus == '被撤回') {
                            that.formObj[2].disabled = true;
                        } else {
                            that.formObj[2].disabled = false;
                        }
                    } else if (that.statusNum == 3) {
                        that.formObj[2].disabled = true;
                        //工程名称
                        if (that.formData.fEngineeringname == null || that.formData.fEngineeringid == null || that.formData.fEngineeringname == "" || that.formData.fEngineeringid == "") {
                            that.formData.fEngineeringid = that.userInfo.companyId;
                            that.formData.fEngineeringname = that.userInfo.companyFullName;
                        }
                        //计划检测日期
                        if (that.formData.fPlantestdates == null) {
                            that.formData.fPlantestdates = that.timeYMD;
                        }
                        //编制人名称
                        if (that.formData.fAuthorid == null || that.formData.fAuthorname == null || that.formData.fAuthorid == "" || that.formData.fAuthorname == "") {
                            that.formData.fAuthorid = that.userInfo.id; // //编制人id
                            that.formData.fAuthorname = that.userInfo.userName; // //编制人
                        }
                    }

                    // 检测依据
                    if (res.data.testTask.fTestbasisname != '') {
                        var fTestnote = []
                        this.formObj[5].options.map(item => {
                            res.data.testTask.fTestbasisname.split(",").map(i => {
                                if (item.value == i) {
                                    fTestnote.push(item)
                                }
                            })
                        })
                    } else {
                        fTestnote = []
                    }
                    // 检测依据
                    // 判定依据
                    if (res.data.testTask.fDecidebasisname != '') {
                        var fJudgetype = []
                        this.formObj[6].options.map(item => {
                            res.data.testTask.fDecidebasisname.split(",").map(i => {
                                if (item.value == i) {
                                    fJudgetype.push(item)
                                }
                            })
                        })
                    } else {
                        fJudgetype = [];
                    }
                    // 判定依据
                    // 设备
                    if (res.data.testTask.fEquipnameandnoid != '') {
                        var fEquipmentname = []
                        this.formObj[7].options.map(item => {
                            res.data.testTask.fEquipnameandno.split(",").map(i => {
                                if (item.label == i) {
                                    fEquipmentname.push(item);
                                }
                            })
                        })
                    } else {
                        fEquipmentname = []
                    }
                    // 设备
                    this.selected = [{
                        name: "fTestbasisnameList",
                        selected: fTestnote,
                    },
                    {
                        name: "fDecidebasisnameList",
                        selected: fJudgetype,
                    },
                    {
                        name: "fEquipnameandnoList",
                        selected: fEquipmentname,
                    },
                    ];

                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },

        selectChange(data, a, b) {
            console.log("selectChange", data, a, b);
            if (b == 'fTestbasisnameList') {
                var arr = [],
                    arrid = []
                data.map(item => {
                    arr.push(item.value);
                    arrid.push(item.label)
                })
                this.formData.fTestbasisname = arr.join(",");
                this.formData.fTestbasisid = arrid.join(",");
            }
            if (b == 'fDecidebasisnameList') {
                var arr1 = [],
                    arr1id = [];
                data.map(item => {
                    arr1.push(item.value);
                    arr1id.push(item.label);
                })
                this.formData.fDecidebasisname = arr1.join(",");
                this.formData.fDecidebasisid = arr1id.join(",");
            }
            if (b == 'fEquipnameandnoList') {
                var arr2 = [],
                    arr2id = [];
                data.map(item => {
                    arr2.push(item.label);
                    arr2id.push(item.value);
                })
                this.formData.fEquipnameandno = arr2.join(",");
                this.formData.fEquipnameandnoid = arr2id.join(",");
            }
            if (b == 'fCompanyname') {
                this.formData.fCompanyname = data.label;
                this.formData.fCompanyid = data.value;
                this.formData.fEngineeringpart = ""
                this.formData.fEngineeringpartid = ""
            }
        },
        inputFocus(index, prop) {
            if (prop == "fWitnessno") {
                this.isShow = true;
            }
            console.log("inputFocus", index, prop);
            return index, prop;
        },
        yj_saveForm() {
            if (this.$refs.publicForm.submitForm()) {
                // console.log("saveForm", this.formData);
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data) {
            this.loading = true;
            this.isLock = true
            var save = {};
            save = {
                fId: data.fId, // 申请单fid //为空判断为新增
                fEngineeringname: data.fEngineeringname, // 工程名称
                fEngineeringid: data.fEngineeringid, // 工程id
                fEngineeringpart: data.fEngineeringpart, // 工程部位
                fEngineeringpartid: data.fEngineeringpartid, // 工程部位id
                fPlantestdates: data.fPlantestdates, // 计划检测日期 年-月-日
                fTestcondition: data.fTestcondition, // 试验条件
                fTestbasisid: data.fTestbasisid, // 检测依据id
                fTestbasisname: data.fTestbasisname, // 检测依据内容
                fDecidebasisid: data.fDecidebasisid, // 判定依据id
                fDecidebasisname: data.fDecidebasisname, // 判定依据内容
                fEquipnameandno: data.fEquipnameandno, // 检测仪器设备名称及编号
                fThicknessdesignvalue: data.fThicknessdesignvalue, // 保护层厚度设计值(mm)
                fAllowdeviation1: data.fAllowdeviation1, // 允许偏差(-mm)
                fAllowdeviation: data.fAllowdeviation, // 允许偏差(+mm)
                fCorrectionfactor: data.fCorrectionfactor, // 修正因子(mm)
                fAuthorid: data.fAuthorid, // 编制人ID
                fAuthorname: data.fAuthorname, // 编制人名称
                companyId: data.fCompanyid, // 当前用户标段ID
                companyName: data.fCompanyname,// 当前用户标段名称
                // supervisionId: this.userInfo.jlCompanyId,// 当前用户监理ID
                // supervisionName: this.userInfo.jlCompanyName, // 当前用户监理名称
                supervisionId: "", // 当前用户监理ID
                supervisionName: "", // 当前用户监理名称
                projectId: this.primaryInfo.getList.projectId, // 当前用户项目ID
                projectName: "", // 当前用户项目名称
                userId: this.userInfo.id, // 当前登录人id
                userName: this.userInfo.userName, // 当前登录人姓名
                fNature: this.userNature,//当前用户机构性质
            };
            saveSteelProtectionTestTask(save)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        // this.$emit("refreshList");
                        this.$router.push({
                            path: "/rebarThicknessManage/rebarDetectionTask",
                        });
                        this.isLock = false
                        this.$message.success({
                            content: '新增成功',
                            duration: 1000
                        })
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });

        },



        // 数据字典
        // 试验检测依据-获取试验依据，检测依据
        getCheckitemsList(num) {
            getCheckitemsList({
                modelType: num,
            })
                .then((res) => {
                    if (res.errcode == 0) {

                        if (num == 5) { //试验依据
                            this.formObj[5].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[5].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId
                                })
                            })

                        } else if (num == 6) { //判定依据
                            this.formObj[6].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[6].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId
                                })
                            })

                        }

                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 主要仪器设备名称及编号
        getEquipment() {
            getMangEquipmentPageList({
                fCompanyid: this.primaryInfo.fCompanyid, //当前登录用户机构id
                keyWord: "", //设备名称模糊查询字段
                keyType: "钢筋保护层厚度检测设备", //设备类型
                fNote: "2",
                modelType: "2", // 0代办 1已办
                sublevel: 0, //查看下级  1是0否
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.formObj[7].options = [];
                        res.data.mangEquipmentList.list.map(item => {
                            this.formObj[7].options.push({
                                label: item.fEqptname + "/" + item.fEqptno,
                                value: item.fId
                            })
                        })
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 数据字典

        // WBS树展示
        clickDialog(index, prop) {
            console.log("clickDialog", index, prop);
            if (prop == "fEngineeringpart") {
                this.WBSShow = true;
            }
            return index, prop;
        },
        getWBS(data) { //获取选择WBS信息
            this.formData.fEngineeringpart = data.fLongname //工程部位
            this.formData.fEngineeringpartid = data.fNodeid //工程部位id
        },
        closeDialog() {
            this.$emit("close");
        },
        WBSDialog() { // WBS树关闭
            this.WBSShow = false;
        },
        // WBS树展示

        getData() {
            var time = new Date();
            const y = time.getFullYear();
            const m = (time.getMonth() + 1 + "").padStart(2, "0");
            const d = (time.getDate() + "").padStart(2, "0");
            const hh = (time.getHours() + "").padStart(2, "0");
            const mm = (time.getMinutes() + "").padStart(2, "0");
            const ss = (time.getSeconds() + "").padStart(2, "0");
            this.timeYMD = `${y}-${m}-${d}`;
            this.timeYmdHms = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        },


        // 获取委托单列表数据
        closeDialog1() {
            this.isShow = false;
        },
        getEntrustRow(data) {
            // console.log("获取委托单列表数据", data);
            this.formData.fCompanyname = data.fCompanyname;//标段名称--施工单位
            this.formData.fCompanyid = data.fCompanyid;//标段名称--标段ID
            this.formData.fEngineeringname = data.fProjectuse;//标段名称--工程名称
            this.formData.fEngineeringid = "";//标段名称--工程名称id
            this.formData.fEngineeringpart = data.fProjecttypename;//工程部位名称
            this.formData.fEngineeringpartid = data.fProjecttypeid;//工程部位ID
            this.formData.fThicknessdesignvalue = data.fThicknessdesignvalue;//保护层厚度设计值
            this.formData.fAllowdeviation = data.fAllowdeviation;//允许偏差+
            this.formData.fAllowdeviation1 = data.fAllowdeviation1;//允许偏差-
            this.formData.fTestbasisid = "";//检测依据ID
            this.formData.fTestbasisname = data.fTestnote;//检测依据内容
            this.formData.fDecidebasisid = "";//判定依据ID
            this.formData.fDecidebasisname = data.fJudgetype;//判定依据名称
            this.formData.fWitnessno = data.fEntrustno;//委托单编号
            this.formData.fWitnessid = data.fId;//委托单id
            // 多选
            // 检测依据
            if (data.fTestnote != '') {
                var fTestnoteList = []
                this.formObj[6].options.map(item => {
                    data.fTestnote.split(",").map(i => {
                        if (item.label == i) {
                            fTestnoteList.push(item)
                        }
                    })
                })
            } else {
                fTestnoteList = []
            }
            // 判定依据
            if (data.fJudgetype != '') {
                var fJudgetypeList = []
                this.formObj[7].options.map(item => {
                    data.fJudgetype.split(",").map(i => {
                        if (item.label == i) {
                            fJudgetypeList.push(item)
                        }
                    })
                })
            } else {
                fJudgetypeList = []
            }

            this.selected = [
                {
                    name: "fTestbasisnameList",
                    selected: fTestnoteList,
                },
                {
                    name: "fDecidebasisnameList",
                    selected: fJudgetypeList,
                }
            ];
            // 多选

        },
        // 获取委托单列表数据
    },
};